package org.bigdata.platform;

public abstract class BaseIteratorFileObject<E> implements IIteratorFileObject<IteratorElement<E>> {

	private IteratorElement<E> head = null;
	
	private IteratorElement<E> current = null;
	
	private IteratorElement<E> tail = null;

	@Override
	public boolean hasNext() {
		return this.current != null;
	}

	@Override
	public IteratorElement<E> next() {
		if(!hasNext()) {
			throw new RuntimeException("No next element present. Please use hasNext() before calling next()");
		}
		try {
			return current;
		} finally {
			this.current = this.current.getNext();
		}
	}

	@Override
	public void remove() {
		throw new RuntimeException("Method not supported: remove()");
	}

	@Override
	public void addElement(IteratorElement<E> element) {
		if(this.head == null) {
			this.head = element;
			this.current = element;
		} else {
			this.tail.setNext(element);
		}
		this.tail = element;
	}

}
